<?php
/**
 * @todo 生产
 * @copyright gaoxiang 2009
 * @author gaoxiang
 * @createdate 2009.03.20
 * @license 本程序遵照Creative Commons 署名-非商业性使用-相同方式共享 2.5 中国大陆 协议，该协议的中文版本可在http://creativecommons.org/licenses/by-nc-sa/2.5/cn/下查看。
 **/
require_once(dirname(dirname(__FILE__)) . '/require/load.php');
header('Cache-control: private, must-revalidate');
check_user();//验证是否登陆
 if($_GET['action']=="prequery"){//查询表单
 	produce_form(date("Y-m-d"),date("Y-m-d"),"",0,"",0,0,0,"",0,0);
 }elseif($_GET['action']=="product_edit_input"){//查询并修改生产记录的表单
 	check_rights(2);//验证是否有权限查看此页
 	produce_edit_form(); 
 }elseif($_GET['action']=="product_edit"){//单个批号输入并修改
 	check_rights(2);
 	if (!is_product($_POST['product']))
		back_prior('请输入正确的产品批号！');
	$product=strtoupper($_POST['product']);
	$query="SELECT cuserid FROM produce where product_id='" . $product. "'";
	$query_user=$database->fetch_row($query);
	if(!empty($query_user)){//如果查询到此批号
		if(substr($_SESSION['rights'],1,1)=='1'){//如果用户是管理员或者技术
			$database->fetch_produce_ee(0,0,0,0,0,0,0,0,$product,0,"0",0);
		}elseif($query_user[0]!=$_SESSION['userid']){//如果用户不是该批号的所有者
			set_refer();
			login_echo($_SESSION['referer'],"对不起，你没有权限修改此批号");
		}else
			$database->fetch_produce_ee(0,0,0,0,0,0,0,0,$product,0,"0",$_SESSION['userid']);
	}else{//如果查询不到此批号
		set_refer();
			login_echo($_SESSION['referer'],"对不起，无此批号");
	}
			
 }elseif($_GET['action']=="prequery_edit"){//输出修改页面的查询条件
 	check_rights(2);
 	produce_form(date("Y-m-d"),date("Y-m-d"),"",0,"",0,0,0,"",0,1);
 }elseif ($_GET['action']=="query"){  //查询
	if(!is_date($_POST['pbegin']) || !is_date($_POST['pend'])){
		back_prior('请输入正确的日期！');
	}
	result_header('查询结果');
 	$database->fetch_produce($_POST['pbegin'],$_POST['pend'],$_POST['pclient'],$_POST['pspec'],$_POST['prog'],$_POST['pmachine'],$_POST['pclass'],$_POST['pgroup'],0,$_POST['pmould'],$_POST['pjudge']); 
 }elseif ($_GET['action']=="product_edit_query"){  //查询并修改
 	check_rights(2);
	if(!is_date($_POST['pbegin']) || !is_date($_POST['pend'])){
		back_prior('请输入正确的日期！');
	}
	if(substr($_SESSION['rights'],1,1)=='1'){//如果用户是管理员或者技术
		$database->fetch_produce_ee($_POST['pbegin'],$_POST['pend'],$_POST['pclient'],$_POST['pspec'],$_POST['prog'],$_POST['pmachine'],$_POST['pclass'],$_POST['pgroup'],0,$_POST['pmould'],$_POST['pjudge'],0); 
	}else
 		$database->fetch_produce_ee($_POST['pbegin'],$_POST['pend'],$_POST['pclient'],$_POST['pspec'],$_POST['prog'],$_POST['pmachine'],$_POST['pclass'],$_POST['pgroup'],0,$_POST['pmould'],$_POST['pjudge'],$_SESSION['userid']); 
 } elseif($_GET['action']=="prepare_edit"){	//预输入
 	check_rights(2);
 	prepare_edit();
 }elseif($_GET['action']=="edit"){		//输入
	check_rights(2);
  	$refer_predit = $database->arr_config_r[0] . "action/produce.php?action=prepare_edit";
	if (!$_POST['preclient'])
		back_prior('请输入客户名称！') ;
	elseif (!is_rog($_POST['prerog']))
		back_prior('请输入正确的投料批号！') ;
	elseif (!is_product($_POST['preproduct']))
		back_prior('请输入正确的产品批号！') ;
	elseif ( !is_number($_POST['prenum'],1))
		back_prior('请输入正确的记录数！') ;
	elseif ( ($_POST['premium'] != "" && !is_number($_POST['premium'],0)) && $_POST['premium']!=0)
		back_prior('请输入正确的溢料重量！') ;
	else{
		if($_POST['premium'] != "" &&  $_POST['premium']!=0){
			$product_dis=strtoupper($_POST['preproduct']);
			$query_dis="SELECT disid FROM disqualification where disproduct='" . substr($product_dis,0,10) . "' LIMIT 1";
			if (mysql_num_rows(mysql_query($query_dis))){
				$insert_dis="UPDATE disqualification SET dispremium='" . $_POST['premium'] . "',uuserid=" . $_SESSION['userid'].  " WHERE disproduct='" . substr($product_dis,0,10) . "' LIMIT 1" ;
			}else{
				$insert_dis="INSERT INTO disqualification value ('',CURRENT_TIMESTAMP,'" . string_to_date($product_dis,'product') . "','" . substr($product_dis,0,10) . "','" . substr($product_dis,8,1) . "','" . substr($product_dis,9,1) . "','" . substr($product_dis,6,2) . "','". $_POST['premium'] ."'," . $_SESSION['userid'] . ",'')" ;
			}
			$database->write_table($insert_dis);
		}
 		produce_edit($_POST['predate'],$_POST['preclient'],$_POST['prespec'],$_POST['prerog'],$_POST['preproduct'],$_POST['prenum']);
 	}
 }elseif($_GET['action']=="write"){  //写入数据库，比较重要，存在大量严格校验
 	//开始校验
 	check_rights(2);
 	if($_POST['tr_rows']<=0){
 		back_prior('没有记录，请返回！') ;
 	}
 	$array_pe=array();
 	for($i = 1;$i<=$_POST['tr_rows'];++$i){
 		//产品批号校验
 		if($_POST['eproduct' . $i]==""){
 			continue;
 		}
		$product_upper=strtoupper($_POST['eproduct' . $i]);//产品批号变大写
		if(!is_product($product_upper))
			back_prior('请输入正确的产品批号！');
		for($j = 0;$j<$_POST['tr_rows'];++$j){//把全部元素都放入数组，然后使用duplicate函数校验
			$product_upper=strtoupper($_POST['eproduct' . ($j+1)]);//产品批号变大写
			$array_pe[$j] = $product_upper;
		}
		if(!duplicate($array_pe))
			back_prior('产品批号输入重复！');//产品批号校验完成
		$check_query = 'SELECT pid FROM produce where product_id="' . $product_upper . '"';
		if(mysql_num_rows(mysql_query($check_query)))
			back_prior('服务器已经有相同产品批号！');
 		if(!$_POST['eclient' . $i])
 			back_prior('请输入客户名称！');
		$rog_upper=strtoupper($_POST['erog' . $i]);//投料批号变大写
		if(!is_rog($rog_upper))
			back_prior('请输入正确的投料批号！');
		if($_POST['eweight' . $i]=="" || !is_number($_POST['eweight' . $i],0))
			back_prior('请输入正确的重量值！') ;
		if (!is_mould($_POST['emould' . $i]))
			back_prior('请输入正确的模具号！') ;
		if( !is_number($_POST['edefect' . $i],1))
			back_prior('请输入正确的伤点数！') ;
		if($_POST['ejudge'.$i]!="合格")
			if($_POST['eremarks'.$i]=="")
				back_prior('不合格及待检品请注明原因！') ;
 	}//结束校验
 	//开始写入
 	$pe_write="INSERT INTO `gdcopper_al`.`produce` (`pid` ,`palter_time` ,`pcreate_time` ,`pclient` ,`pspec` ,`prog` ,`pmachine` ,`pclass` ,`pgroup` ,`product_id` ,`pmould` ,`pweight` ,`pdefect` ,`psurface` ,`pjudge` ,`premarks`,`cuserid`,`uuserid`) VALUES ";
 	for ($i=1;$i<=$_POST['tr_rows'];++$i){
 		if($_POST['eproduct' . $i]==""){
 			continue;
 		}
 		$product_upper=strtoupper($_POST['eproduct' . $i]);
 		$rog_upper=strtoupper($_POST['erog' . $i]);//投料批号变大写
 		if (compare_date($product_upper,time())){//如果生产批号表现的日期和现有日期相同，则直接用现在时间 
 			$pe_write .="(NULL ,CURRENT_TIMESTAMP , " . "'" . date('Y-m-d H:i:s',time()) . "','" . $_POST['eclient' . $i] . "','" . $_POST['espec' . $i] . "','" . $rog_upper . "','" . substr($product_upper,6,2) . "','" . substr($product_upper,8,1) . "','" . substr($product_upper,9,1) . "','" . $product_upper . "','" . $_POST['emould' . $i] . "','" . $_POST['eweight' . $i] . "','" .$_POST['edefect' . $i] . "','" . $_POST['esurface' . $i] . "','" . $_POST['ejudge' . $i] ."','" .$_POST['eremarks' . $i] . "'," . $_SESSION['userid'] . ",''),";
 		}else{
 			$date_jie=string_to_date($product_upper,'product');
 			$pe_write .="(NULL ,CURRENT_TIMESTAMP , " . "'" . $date_jie . "','" . $_POST['eclient' . $i] . "','" . $_POST['espec' . $i] . "','" . $rog_upper . "','" . substr($product_upper,6,2) . "','" . substr($product_upper,8,1) . "','" . substr($product_upper,9,1) . "','" . $product_upper . "','" . $_POST['emould' . $i] . "','" . $_POST['eweight' . $i] . "','" .$_POST['edefect' . $i] . "','" . $_POST['esurface' . $i] . "','" . $_POST['ejudge' . $i] ."','" .$_POST['eremarks' . $i] . "'," . $_SESSION['userid'] . ",''),";
		}
 	}
 	$pe_write = substr($pe_write,0,strlen($pe_write)-1);
 	if($database->write_table($pe_write)){
 	result_header('添加成功，结果如下：');
 	$database->fetch_produce(0,0,0,0,0,'00',0,0,0,0,"0",mysql_insert_id(),mysql_affected_rows());
 	}else{
 		set_refer();
 		login_echo($referer,"添加失败，请通知管理员");
	}
 }elseif($_GET['action']=='inspection'){//如果要进入待检品页面
 	if(substr($_SESSION['rights'],1,1)=='1'){
 		$database->fetch_inspection(3);
 	}else{
 		$database->fetch_inspection(2);
 	}
 }elseif($_GET['action']=='packedit'){//如果要进入问题品提交页面
 	pack_edit();
 }elseif($_GET['action']=='packsubmit'){
 	for($i=1;isset($_POST['eproduct' . $i]);++$i){
 		if($_POST['eproduct' . $i] =="")
 			continue;
		if(!is_product($_POST['eproduct' . $i])){
			set_refer();
			login_echo($_SESSION['referer'],"批号不符合规则，请重新输入");
		}else{
			if($_POST['eremarks' . $i] ==""){
				set_refer();
				login_echo($_SESSION['referer'],"请注明原因");
			}
			$query="SELECT pid FROM produce WHERE product_id='" . $_POST['eproduct' . $i] . "' LIMIT 1";
			if(!mysql_num_rows(mysql_query($query))){
				set_refer();
				login_echo($_SESSION['referer'],"生产工段无此批号，请检查后修改");
			}else{
				$query1="SELECT id FROM pack WHERE product='" . $_POST['eproduct' . $i] . "' LIMIT 1";
				if(mysql_num_rows(mysql_query($query1))){
					$write="UPDATE pack SET alter_time= CURRENT_TIMESTAMP,process='" . $_POST['epack' . $i]. "',judge='" . $_POST['ejudge' . $i] . "',remarks='" . $_POST['eremarks' . $i] . "',uuserid=" . $_SESSION[userid] . " WHERE product='" . $_POST['eproduct' . $i] . "' LIMIT 1";
				}else
					$write="INSERT INTO pack VALUES ('','" . date('Y-m-d H:i:s',time()) ."',CURRENT_TIMESTAMP,'" . $_POST['eproduct' . $i]. "','" . $_POST['epack' . $i]. "','" . $_POST['ejudge' . $i] . "','" . $_POST['eremarks' . $i] . "'," . $_SESSION[userid] . ",'')";
				if($database->write_table($write)){
					login_echo($database->arr_config_r[0],"添加成功，请返回首页");
				}else{
					login_echo($database->arr_config_r[0],"服务器出错，添加失败，请通知管理员");
				}
			}		
		}
 	}
	set_refer();
	login_echo($_SESSION['referer'],"请输入数据！");
 }
html_footer();
?>